package com.py.credit.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.py.credit.common.logs.ExceptionLogger;
import com.py.credit.common.rest.RestServiceResult;
import com.py.credit.service.DataChooseTableService;
import com.py.framework.core.annotation.IgnoreAuthority;
import com.py.framework.core.common.Page;

@RestController
@RequestMapping(value = "/dataChooseTable")
public class DataChooseTableController extends BaseController {
	@Autowired
	private DataChooseTableService dataChooseTableService;

	/**
	 * @api {get} /dataChooseTable/getDataChooseTableForPage.json 一站式查询
	 * @apiName DataChooseTable
	 * @apiVersion 2.3
	 * @apiGroup
	 * 
	 * @apiParam {String} input 输入信息
	 * @apiSuccess {String} code 状态码:1 成功 -1 失败
	 * @apiSuccess {String} msg 错误信息
	 * @apiSuccess {int} count 结果集数据量
	 * @apiSuccess {Object[]} data 结果集
	 *
	 * @apiSuccessExample {json} 成功返回结果
	 *                    {"code":0,"msg":null,"count":5841,"data":[{"tybm":"0252250c19994b649a796a082c318fd5","A000001":"安图县鸿茗房地产开发有限公司","A000002":"912224000000000000"},
	 *                    {"tybm":"e3b70be60094478480975ee373eb92d9","A000001":"吉林省森舟一帆风顺酒店管理有限公司","A000002":"91220621068615693K"}]}
	 */
	@IgnoreAuthority
	@RequestMapping(value = "/getDataChooseTableForPage.json")
	public RestServiceResult<List<Map<String, Object>>> DataChooseTableForPage(
			@RequestParam(value = "input", required = true) String input,
			@RequestParam(value = "code", required = true) String code, Page page) {
		RestServiceResult<List<Map<String, Object>>> r = new RestServiceResult<List<Map<String, Object>>>();
		List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
		if (!StringUtils.isEmpty(input)) {
			try {
				// TODO %input%查询速度太慢 60万数据需要6s加上查询数量需要12s 1.分表 2.前置迷糊 索引
				mapList = dataChooseTableService.DataChooseOneTableForPage(input, code, page);
				r.setCount(page.getTotalCount());
			} catch (Exception e) {
				r.setMsg("查询失败");
				r.setCode(RestServiceResult.CODE_UNKNOW);
				ExceptionLogger.error("DataChooseTableController#DataChooseTable", "搜索查询失败");
			}
			r.setData(mapList);
		}
		return r;
	}

	/**
	 * @api {get} /dataChooseTable/selectDataCount.json 查询数据总量
	 * @apiName selectDataCount
	 * @apiVersion 2.3
	 * @apiGroup
	 * 
	 * 
	 * @apiSuccess {String} code 状态码:1 成功 -1 失败
	 * @apiSuccess {String} msg 错误信息
	 * @apiSuccess {int} count 结果集数据量
	 * @apiSuccess {Object[]} data 结果集
	 *
	 * @apiSuccessExample {json} 成功返回结果
	 *                    {"code":0,"msg":null,"count":0,"data":{"归集各类信息":0,"收录信息目录":125}}
	 */
	@RequestMapping(value = "/selectDataCount.json")
	public RestServiceResult<Map<String, Integer>> selectDataCount() {
		RestServiceResult<Map<String, Integer>> r = new RestServiceResult<Map<String, Integer>>();
		Map<String, Integer> m = new HashMap<String, Integer>();
		try {
			m = dataChooseTableService.selectAllDataCount();
		} catch (Exception e) {
			r.setCode(RestServiceResult.CODE_UNKNOW);
			r.setMsg("查询数据失败");
			ExceptionLogger.error("DataChooseTableController#selectDataCountForPage", "搜索查询失败");
		}
		r.setData(m);
		return r;
	}

	/**
	 * @api {get} /dataChooseTable/selectEvMenCount.json 查询一站式目录数据
	 * @apiName selectEvMenCount
	 * @apiVersion 2.3
	 * @apiGroup
	 * 
	 * 
	 * @apiSuccess {String} code 状态码:1 成功 -1 失败
	 * @apiSuccess {String} msg 错误信息
	 * @apiSuccess {int} count 结果集数据量
	 * @apiSuccess {Object[]} data 结果集
	 *
	 * @apiSuccessExample {json} 成功返回结果
	 *
	 */
	@RequestMapping(value = "/selectEvMenCount.json")
	public RestServiceResult<List<Map<Object, Object>>> selectEvMenCount() {
		RestServiceResult<List<Map<Object, Object>>> r = new RestServiceResult<List<Map<Object, Object>>>();
		List<Map<Object, Object>> m = new ArrayList<Map<Object, Object>>();
		try {
			m = dataChooseTableService.selectEvMenCount();
		} catch (Exception e) {
			r.setCode(RestServiceResult.CODE_UNKNOW);
			r.setMsg("查询数据失败");
			ExceptionLogger.error("DataChooseTableController#selectEvMenCount", "一站式目录数查询失败");
		}
		r.setData(m);
		return r;
	}

	/**
	 * @api {get} /dataChooseTable/selectDeptDataCount.json 查询部门更新后数据
	 * @apiName selectDeptDataCount
	 * @apiVersion 2.3
	 * @apiGroup
	 * 
	 * 
	 * @apiSuccess {String} code 状态码:1 成功 -1 失败
	 * @apiSuccess {String} msg 错误信息
	 * @apiSuccess {int} count 结果集数据量
	 * @apiSuccess {Object[]} data 结果集
	 *
	 * @apiSuccessExample {json} 成功返回结果
	 *                    {"code":0,"msg":null,"count":48,"data":[{"OFFICE_NAME":"工商局","RESOURCE_TITLE":"吉林省特级教师名单","SCOUNT":0,"CREDIT_TIME":"2018-09-15"},{"OFFICE_NAME":"工商局","RESOURCE_TITLE":"自然人红名单","SCOUNT":0,"CREDIT_TIME":"2018-09-15"},{"OFFICE_NAME":"工商局","RESOURCE_TITLE":"个人基本信息","SCOUNT":0,"CREDIT_TIME":"2018-09-15"},{"OFFICE_NAME":"工商局","RESOURCE_TITLE":"教师基本信息","SCOUNT":0,"CREDIT_TIME":"2018-09-15"},{"OFFICE_NAME":"市直属公积金","RESOURCE_TITLE":"法人红名单","SCOUNT":0,"CREDIT_TIME":"2018-09-14"},{"OFFICE_NAME":"市直属公积金","RESOURCE_TITLE":"法人黑名单","SCOUNT":0,"CREDIT_TIME":"2018-09-14"}]}
	 *
	 */
	@RequestMapping(value = "/selectDeptDataCount.json")
	public RestServiceResult<List<Map<Object, Object>>> selectDeptDataCount(Page page) {
		RestServiceResult<List<Map<Object, Object>>> r = new RestServiceResult<List<Map<Object, Object>>>();
		List<Map<Object, Object>> m = new ArrayList<Map<Object, Object>>();
		try {
			m = dataChooseTableService.selectDeptDataCountForPage(page);
			r.setCount(page.getTotalCount());
		} catch (Exception e) {
			r.setCode(RestServiceResult.CODE_UNKNOW);
			r.setMsg("查询数据失败");
			ExceptionLogger.error("DataChooseTableController#selectDeptDataCount", "部门更新后数据查询失败");
		}
		r.setData(m);
		return r;

	}

	/**
	 * @api {get} /dataChooseTable/selectPDCount.json 查询频道首页数据查询
	 * @apiName selectPDCount
	 * @apiVersion 2.3
	 * @apiGroup
	 * 
	 * 
	 * @apiSuccess {String} code 状态码:1 成功 -1 失败
	 * @apiSuccess {String} msg 错误信息
	 * @apiSuccess {int} count 结果集数据量
	 * @apiSuccess {Object[]} data 结果集
	 *
	 * @apiSuccessExample {json} 成功返回结果
	 *                    {"code":0,"msg":null,"count":0,"data":{"归集总数":0,"当月归集总数":2072692}}
	 *
	 */
	@RequestMapping(value = "/selectPDCount.json")
	public RestServiceResult<Map<Object, Integer>> selectPDCount() {
		RestServiceResult<Map<Object, Integer>> r = new RestServiceResult<Map<Object, Integer>>();
		Map<Object, Integer> map = new HashMap<Object, Integer>();
		try {
			map = dataChooseTableService.selectAllAndMonthCount();
		} catch (Exception e) {
			r.setCode(RestServiceResult.CODE_UNKNOW);
			r.setMsg("查询数据失败");
			ExceptionLogger.error("DataChooseTableController#selectPDCount", "频道首页数据查询失败");
		}
		r.setData(map);
		return r;
	}

	/**
	 * @api {get} /dataChooseTable/selectGJEntityCount.json 查询归集实体数据查询
	 * @apiName selectGJEntityCount
	 * @apiVersion 2.3
	 * @apiGroup
	 * 
	 * 
	 * @apiSuccess {String} code 状态码:1 成功 -1 失败
	 * @apiSuccess {String} msg 错误信息
	 * @apiSuccess {int} count 结果集数据量
	 * @apiSuccess {Object[]} data 结果集
	 *
	 * @apiSuccessExample {json} 成功返回结果
	 *                    {"code":0,"msg":null,"count":0,"data":[{"归集各类条数":0}]}
	 *
	 */
	@RequestMapping(value = "selectGJEntityCount.json")
	public RestServiceResult<List<Map<Object, Object>>> selectGJEntityCount() {
		RestServiceResult<List<Map<Object, Object>>> r = new RestServiceResult<List<Map<Object, Object>>>();
		List<Map<Object, Object>> list = new ArrayList<Map<Object, Object>>();
		try {
			list = dataChooseTableService.selectGJEntityCount();
		} catch (Exception e) {
			r.setCode(RestServiceResult.CODE_UNKNOW);
			r.setMsg("实体数据数据查询失败");
			ExceptionLogger.error("DataChooseTableController#selectGJEntityCount", "实体数据数据查询失败");
		}
		r.setData(list);
		return r;
	}

	/**
	 * @api {get} /dataChooseTable/selectPSACount.json 统计归集 公开属性
	 *      P:社会公开,S:政务共享,A:授权查询
	 * @apiName selectPSACount
	 * @apiVersion 2.3
	 * @apiGroup
	 * 
	 * 
	 * @apiSuccess {String} code 状态码:1 成功 -1 失败
	 * @apiSuccess {String} msg 错误信息
	 * @apiSuccess {int} count 结果集数据量
	 * @apiSuccess {Object[]} data 结果集
	 *
	 * @apiSuccessExample {json} 成功返回结果 {"code":0,"msg":"公开属性
	 *                    P:社会公开,S:政务共享,A:授权查询","count":0,"data":[{"sharing_type":"P","SUM(scount)":0},{"sharing_type":"A","SUM(scount)":0},{"归集各类条数":0}]}
	 *
	 */
	@RequestMapping(value = "selectPSACount.json")
	public RestServiceResult<List<Map<Object, Object>>> selectPSACount() {
		RestServiceResult<List<Map<Object, Object>>> r = new RestServiceResult<List<Map<Object, Object>>>();
		List<Map<Object, Object>> list = new ArrayList<Map<Object, Object>>();
		try {
			list = dataChooseTableService.selectPSACount();
			r.setMsg("公开属性 P:社会公开,S:政务共享,A:授权查询");
		} catch (Exception e) {
			r.setCode(RestServiceResult.CODE_UNKNOW);
			r.setMsg(" 公开属性 P:社会公开,S:政务共享,A:授权查询 失败！");
			ExceptionLogger.error("DataChooseTableController#selectPSACount", " 公开属性 P:社会公开,S:政务共享,A:授权查询");
		}
		r.setData(list);
		return r;
	}

	/**
	 * @api {get} /dataChooseTable/selectDeptCount.json 部门数据归集数量查询
	 * @apiName selectDeptCount
	 * @apiVersion 2.3
	 * @apiGroup
	 * 
	 * 
	 * @apiSuccess {String} code 状态码:1 成功 -1 失败
	 * @apiSuccess {String} msg 错误信息
	 * @apiSuccess {int} count 结果集数据量
	 * @apiSuccess {Object[]} data 结果集
	 *
	 * @apiSuccessExample {json} 成功返回结果
	 *                    {"code":0,"msg":null,"count":0,"data":[{"OFFICE_NAME":"诸暨市民政部","SUM(scount)":0},{"OFFICE_NAME":"市直属工商部门","SUM(scount)":0},{"OFFICE_NAME":"诸暨市土地局","SUM(scount)":0},{"OFFICE_NAME":"工商局","SUM(scount)":0},{"OFFICE_NAME":"市直属公积金","SUM(scount)":0},{"OFFICE_NAME":"新昌区铁路局","SUM(scount)":0},{"OFFICE_NAME":"发改委1","SUM(scount)":0},{"OFFICE_NAME":"发改委","SUM(scount)":0},{"OFFICE_NAME":"新昌区人事部","SUM(scount)":0},{"OFFICE_NAME":"社保局","SUM(scount)":0},{"OFFICE_NAME":"越城区劳保局","SUM(scount)":0},{"OFFICE_NAME":"嵊州市土地局","SUM(scount)":0},{"OFFICE_NAME":"越城区公安局","SUM(scount)":0},{"OFFICE_NAME":"上虞市人事部","SUM(scount)":0},{"OFFICE_NAME":"越城区财务部","SUM(scount)":0}]}
	 *
	 */
	@RequestMapping(value = "selectDeptCount.json")
	public RestServiceResult<List<Map<Object, Object>>> selectDeptCount() {
		RestServiceResult<List<Map<Object, Object>>> r = new RestServiceResult<List<Map<Object, Object>>>();
		List<Map<Object, Object>> list = new ArrayList<Map<Object, Object>>();
		try {
			list = dataChooseTableService.selectDeptCount();
		} catch (Exception e) {
			r.setCode(RestServiceResult.CODE_UNKNOW);
			r.setMsg(" 部门数据归集数量查询失败");
			ExceptionLogger.error("DataChooseTableController#selectDeptCount", "部门数据归集数量查询失败");
		}
		r.setData(list);
		return r;
	}
}
